﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.STMSapp
{
    public static class FormUtil
    {
        public static Form GetOpenForm(String frmName)
        {
            Form frm;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == frmName)
                {
                    frm = f;
                    return frm;
                }
            }
            return null;
        }
        // 检查Form是否被打开
        public static bool CheckOpenForm(string frmName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == frmName)
                {
                    bl = true;
                    break;
                }
            }
            return bl;
        }

        /// <summary>
        /// 将指定的Form显示到Panel之中
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="frm"></param>
        public static void AddPanelForm(this Panel panel, Form frm)
        {
            // 检查Form是否被打开
            if (CheckOpenForm(frm.Name))
            {
                Form f = GetOpenForm(frm.Name);
                bool bl = false;
                // 判断当前显示的Form是否已经显示过了
                foreach (Control c in panel.Controls)
                {
                    if (c is Form)
                    {
                        Form cform = c as Form;
                        if (cform.Name.Equals(f.Name))
                        {
                            frm = cform;
                            bl = true;
                            return;
                        }
                    }
                }
                if (!bl)
                {
                    frm = f;
                }
            }
            panel.Controls.Clear();
            frm.TopLevel = false;
           
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            panel.AutoScroll = true;
            panel.Controls.Add(frm);
            frm.Show();
        }
        
        public static void ShowDgvCols(this DataGridView dgv, bool isDel)
        {
            dgv.Columns["colRecover"].Visible = isDel;
            dgv.Columns["colUpdate"].Visible = !isDel;
            dgv.Columns["colDel"].Visible = !isDel;
            dgv.Columns["colRemove"].Visible = !isDel;
        }
    }
}
